package com.yami.shop.rights.common.listener;

import cn.hutool.core.util.ObjectUtil;
import com.yami.shop.bean.event.CancelOrderEvent;
import com.yami.shop.bean.model.OrderItem;
import com.yami.shop.rights.common.strategy.PersonRightsFactory;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

/**
 * 取消订单事件
 * @author lsr
 */
@Component("rightsCancelOrderListener")
@AllArgsConstructor
@Log4j2
public class CancelOrderListener {

    @Autowired
    private PersonRightsFactory personRightsFactory;

    @EventListener(CancelOrderEvent.class)
    public void rightsCancelOrderEvent(CancelOrderEvent event) {
        log.info("定时任务取消订单,权益回滚");
        // 将权益次数回滚
        for(OrderItem orderItem : event.getOrder().getOrderItems()){
            if(ObjectUtil.isNotEmpty(orderItem.getQualificationId()) && orderItem.getQualificationId() != 0){
                personRightsFactory.cancelOrder(orderItem.getQualificationId(),"9");
            }
        }
    }
}
